﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Creator : MonoBehaviour {
   
    public int count = 10;
    private float myTime = 0.5f;
    public Vector3 spawnPosition;

    public GameObject parent;
    public GameObject list;
    public static Queue<GameObject> st;

    List<GameObject> lists = new List<GameObject>();
    List<SpriteRenderer> back = new List<SpriteRenderer>();
    List<SpriteRenderer> front = new List<SpriteRenderer>();
    // Use this for initialization
    void Start()
    {        
        lists = ObjectsPool.instance.Create(parent.transform, list, count);

        foreach(var item in lists)
        {
            back.Add( item.GetComponent<SpriteRenderer>());
            front.Add(item.transform.GetChild(0).GetComponent<SpriteRenderer>());
        }

       // spawnPosition = new Vector3(0, -7 , 0);
        Control.curr = lists[0];
        st = new Queue<GameObject>(lists.Count);
        InvokeRepeating("Moving", myTime, myTime);
    }
    //public static GameObject curr;

    int temp = 0;
   void Moving()
    {
        
       for (int i = 0; i < lists.Count; i++)
        {

            if (!lists[i].activeInHierarchy)
            {
                ObjectsPool.instance.Spawn(lists[i], spawnPosition, Quaternion.identity);
                if (temp == 1)
                    st.Enqueue(lists[i]);
                else temp++;

                ////
               for (int j = 0; j < lists.Count; j++)
                {
                    if (lists[j].activeInHierarchy)
                    {
                        if (j != i)
                        {
                            back[j].sortingOrder++;
                            front[j].sortingOrder++;
                          //  lists[j].GetComponent<SpriteRenderer>().sortingOrder++;
                         //   lists[j].transform.GetChild(0).GetComponent<SpriteRenderer>().sortingOrder++;
                        }
                        else
                        {
                            back[j].sortingOrder = 0;
                            front[j].sortingOrder = 0;
                           // lists[j].GetComponent<SpriteRenderer>().sortingOrder = 0;
                           // lists[j].transform.GetChild(0).GetComponent<SpriteRenderer>().sortingOrder = 0;
                        }
                    }
                }              
                break;
            }
        }
    }

}
